<template>
  <div class="login">
    <div class="left">
      <div class="title">
        <img src="@/assets/login-logo.png" alt />
        <span class="mm">黑马面面</span>
        <span class="line"></span>
        <span class="user">用户登录</span>
      </div>
      <el-form :model="form" class="form" :rules="rules" ref="form">
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            prefix-icon="el-icon-user"
            placeholder="请输入手机号码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            suffix-icon="el-icon-view"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                v-model="form.code"
                prefix-icon="el-icon-key"
                placeholder="请输入验证码"
              ></el-input>
            </el-col>
            <el-col :span="8">
              <img
                class="code"
                :src="baseUrl + codebase"
                alt
                @click="changeCode"
              />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item class="sel" prop="isPart">
          <el-checkbox v-model="form.isPart">
            我已阅读并同意
            <el-link type="primary" class="argee">用户协议</el-link>和
            <el-link type="primary" class="argee">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="tologin"
            >登录</el-button
          >
          <i></i>
          <el-button type="primary" class="btn" @click="toregiest"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/login-right.png" alt />
    </div>
    <register ref="register"></register>
  </div>
</template>

<script>
// 组件套用组件   1. 导入   2. 注册   3. 使用
import register from "./register";
import { loginApi } from "@/api/login.js";
import { saveLocal, getLocal } from "@/utils/local.js";

export default {
  components: {
    register, //register:register
  },
  data() {
    return {
      baseUrl: "",
      codebase: "/captcha?type=login",

      form: {
        phone: "",
        password: "",
        code: "",
        isPart: false,
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              // console.log(value);
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (reg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确手机号"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 6, max: 12, message: "请输入正确密码", trigger: "change" },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          { min: 4, max: 4, message: "请输入4位验证码", trigger: "change" },
        ],
        isPart: [
          { required: true, message: "请勾选协议", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              // console.log(value);
              if (value) {
                callback();
              } else {
                callback(new Error("请勾选协议"));
              }
            },
          },
        ],
      },
    };
  },
  methods: {
    tologin() {
      this.$refs.form.validate((result) => {
        if (result) {
          loginApi(this.form).then((res) => {
          this.$message.success("傻猪猪，登录成功");
            console.log(res);
            // localStorage.setItem('key', res.data.data.token)
            saveLocal('token', res.data.data.token)

            // 登录成功跳转页面
            this.$router.push('/layout')

          });
        } else {
          this.$message.error("傻猪猪，登录失败");
        }
      });
    },
    toregiest() {
      // console.log(this.$refs.register);
      this.$refs.register.isShow = true;
    },
    changeCode() {
      this.codebase = "/captcha?type=login&id=" + Date.now();
    },
  },
  created() {
    this.baseUrl = process.env.VUE_APP_URL;

    // 有token就跳转至layout，没有就不理
    console.log('获取token',getLocal('token'));
    if(getLocal('token')) {
      this.$router.push('/layout')
    }
  },
};
</script>

<style lang="less">
.login {
  width: 100%;
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;

  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 42px;

    .title {
      display: flex;
      align-items: center;
      .mm,
      .user {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
      .mm {
        margin: 0 14px;
      }
      .user {
        margin-left: 14px;
      }
      .line {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
      }
    }
    .form {
      padding-top: 40px;

      .code {
        width: 100%;
        height: 60px;
      }
      .argee {
        vertical-align: top;
      }
      .btn {
        display: block;
        width: 100%;
        &:first-child {
          margin-bottom: 20px;
        }
      }
    }
    .el-input__inner {
      height: 60px;
      line-height: 60px;
    }
    .sel {
      margin-top: 15px;
    }
  }
}
</style>
